#!/usr/bin/env python
"""

    split_file_name.py
    [--log_file PATH]
    [--verbose]

"""

################################################################################
#
#   split_file_name
#
#
#   Copyright (c) 8/8/2011 Leo Goodstadt
#
#   Permission is hereby granted, free of charge, to any person obtaining a copy
#   of this software and associated documentation files (the "Software"), to deal
#   in the Software without restriction, including without limitation the rights
#   to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
#   copies of the Software, and to permit persons to whom the Software is
#   furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included in
#   all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
#   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
#   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
#   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
#   OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
#   THE SOFTWARE.
#################################################################################

import sys, os


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Functions


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

def split_file_name(file_name, levels = 10000):
    nested_path = []
    cnt_levels = 0
    while 1:
        path_part, file_part = os.path.split(file_name)
        #
        # probably at root: stop
        if not len(file_part):
            if len(path_part):
                nested_path.insert(0, path_part)
            break
        #
        nested_path.insert(0, file_part)
        #
        # too many levels: stop
        cnt_levels += 1
        if cnt_levels >= levels:
            nested_path.insert(0, path_part)
            break
        #
        # No more path to split
        if len(path_part) == 0:
            break
        file_name = path_part
    return nested_path

#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

#   Main logic


#88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888
if __name__ == '__main__':
#   debug code not run if called as a module
#
    #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

    #   Testing


    #88888888888888888888888888888888888888888888888888888888888888888888888888888888888888888

    import unittest
    class Test_split_file_name(unittest.TestCase):

        #       self.assertEqual(self.seq, range(10))
        #       self.assert_(element in self.seq)
        #       self.assertRaises(ValueError, random.sample, self.seq, 20)



        def test_function(self):
            """
                test
            """
            self.assertEqual(split_file_name("/a/b/c/d/e/f/gg/hhh/iii.j"),
                             ['/', 'a', 'b', 'c', 'd', 'e', 'f', 'gg', 'hhh', 'iii.j'])
            self.assertEqual(split_file_name("/a/b/c/d/e/f/gg/hhh/iii.j", 1),
                             ['/a/b/c/d/e/f/gg/hhh', 'iii.j'])
            self.assertEqual(split_file_name("/a/b/c/d/e/f/gg/hhh/iii.j", 9),
                             split_file_name("/a/b/c/d/e/f/gg/hhh/iii.j", 10))


    #
    #   call unit test without parameters
    #

    if sys.argv.count("--debug"):
        sys.argv.remove("--debug")
    unittest.main()









